<template>
  <div>
    <van-nav-bar left-arrow @click-left="leftCilck"></van-nav-bar>
    <div class="login-form">
      <h3 class="title">短信登录</h3>
      <van-form ref="form">
        <van-field
          name="mobile"
          v-model="form.mobile"
          :rules="rules.mobile"
          placeholder="请输入手机号"
        ></van-field>
        <van-field
          v-model="form.code"
          :rules="rules.code"
          placeholder="请输入验证码"
        >
          <template #button>
            <div class="code" @click="sendCode">
              发送验证码
            </div>
          </template>
        </van-field>
        <van-button class="btn" @click="submit">登录</van-button>
      </van-form>
    </div>
  </div>
</template>

<script>
import { setItem } from '@/utils/puth.js'
import { authorizations, smsCodes } from '@/api/user.js'
export default {
  data () {
    return {
      form: {
        mobile: 13200000000,
        code: 246810
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'onChange' },
          {
            validator: value => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              return reg.test(value)
            },
            message: '请正确输入手机号',
            trigger: 'onChange'
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'onChange' },
          {
            validator: value => {
              const reg = /^\d{6}$/
              return reg.test(value)
            },
            message: '请正确输入验证码',
            trigger: 'onChange'
          }
        ]
      },
      totalTime: 6, // 初始时间
      interval: '' // 计时器
    }
  },
  methods: {
    leftCilck () {},
    sendCode () {
      this.$refs.form.validate('mobile').then(async () => {
        // 倒计时,点击之后立即开始
        this.totalTime--
        // 设置计时器(异步)
        this.interval = setInterval(() => {
          console.log(this.totalTime)
          this.totalTime-- // 每次减一
          if (this.totalTime <= 0) {
            // 减到0清除计时器
            clearInterval(this.interval)
            this.totalTime = 6
          }
        }, 1000)
        await smsCodes(this.form.mobile)
        // console.log(res)
        this.$toast.success('验证成功')
      })
      // .catch(() => this.$toast.fail('验证失败'))
    },
    submit () {
      this.$refs.form.validate().then(async () => {
        this.$toast.loading({ duration: 0 })
        const res = await authorizations(this.form)
        setItem(res.data.token)
        this.$store.commit('user/setToken', res.data.token)
        const redirect = this.$route.query.redirect || '/'
        this.$router.push(redirect)
        // console.log(res.data.token)
        this.$toast.success('登录成功')
      })
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .van-nav-bar .van-icon {
  color: #999;
}
.login-form {
  padding: 20px;
}
.title {
  font-size: 18px;
  font-weight: 400;
  padding-left: 15px;
  margin: 4vw;
}
.code {
  font-size: 14px;
  color: #ccc;
}
.btn {
  width: 100%;
  background-color: skyblue;
}
</style>
